package util.dialog;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.LinearLayout;

import com.wq.cycling.R;

import util.KickBackAnimator;

/**
 * 通用dialog
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */

public class DialogForMenu {

	private Context context;
	private Dialog dialog;
	private Handler mHandler = new Handler();
	private View img01,img02,img03,img04,img05,img06,close;

	public DialogForMenu(Context context) {
		super();
		this.context = context;
		// TODO Auto-generated constructor stub
	}

	public void show() {
		dialog = new Dialog(context, R.style.my_dialog_style);
		dialog.setCancelable(true);
		final View view = LayoutInflater.from(context).inflate(R.layout.dialog_for_menu, null);
		dialog.setContentView(view, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));
		dialog.show();
		img01 = view.findViewById(R.id.img_01);
		img02 = view.findViewById(R.id.img_02);
		img03 = view.findViewById(R.id.img_03);
		img04 = view.findViewById(R.id.img_04);
		img05 = view.findViewById(R.id.img_05);
		img06 = view.findViewById(R.id.img_06);
		img01.setOnClickListener(listener);
		img02.setOnClickListener(listener);
		img03.setOnClickListener(listener);
		img04.setOnClickListener(listener);
		img05.setOnClickListener(listener);
		img06.setOnClickListener(listener);
		close = view.findViewById(R.id.btn_dialog);
		// 这几个show和close的操作千万不要用集合循环去操作，否则在显示dialog时会出现dialog闪一下就消失的情况
		showAnim(img01, 150);
		showAnim(img02, 200);
		showAnim(img03, 250);
		showAnim(img04, 150);
		showAnim(img05, 200);
		showAnim(img06, 250);

		close.setVisibility(View.VISIBLE);
		AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f);
		animation.setDuration(1000);
		close.setAnimation(animation);

		close.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				close(v);
			}
		});

	}

	private void close(View v){
		v.setVisibility(View.GONE);
		AlphaAnimation animation = new AlphaAnimation(1.0f, 0.1f);
		animation.setDuration(200);
		v.setAnimation(animation);
		closeAnim(img06, 100, 380);
		closeAnim(img05, 150, 430);
		closeAnim(img04, 200, 480);
		closeAnim(img03, 100, 380);
		closeAnim(img02, 150, 430);
		closeAnim(img01, 200, 480);
	}

	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
				case R.id.img_01:
					sure.onSure(1);
					close(close);
					break;
				case R.id.img_02:
					sure.onSure(2);
					close(close);
					break;
				case R.id.img_03:
					sure.onSure(3);
					close(close);
					break;
				case R.id.img_04:
					sure.onSure(4);
					close(close);
					break;
				case R.id.img_05:
					sure.onSure(5);
					close(close);
					break;
				case R.id.img_06:
					sure.onSure(6);
					close(close);
					break;
			}
		}
	};
	private void showAnim(final View i, int d) {
		i.setVisibility(View.INVISIBLE);
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				i.setVisibility(View.VISIBLE);
				ValueAnimator fadeAnim = ObjectAnimator.ofFloat(i, "translationY", 1000, 0);
				fadeAnim.setDuration(600);
				KickBackAnimator kickAnimator = new KickBackAnimator();
				kickAnimator.setDuration(600);
				fadeAnim.setEvaluator(kickAnimator);
				fadeAnim.start();
				fadeAnim.addListener(new Animator.AnimatorListener() {
					@Override
					public void onAnimationStart(Animator animation) {}
					@Override
					public void onAnimationEnd(Animator animation) {
						i.clearAnimation();
					}
					@Override
					public void onAnimationCancel(Animator animation) {}
					@Override
					public void onAnimationRepeat(Animator animation) {}
				});
			}
		},  d);
	}

	private void closeAnim(final View img, int i, int j) {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				ValueAnimator fadeAnim = ObjectAnimator.ofFloat(img, "translationY", 0, 1000);
				fadeAnim.setDuration(600);
				KickBackAnimator kickAnimator = new KickBackAnimator();
				kickAnimator.setDuration(600);
				fadeAnim.setEvaluator(kickAnimator);
				fadeAnim.start();
				fadeAnim.addListener(new Animator.AnimatorListener() {
					@Override
					public void onAnimationStart(Animator animation) {
						// TODO Auto-generated method stub
					}
					@Override
					public void onAnimationRepeat(Animator animation) {
						// TODO Auto-generated method stub
					}
					@Override
					public void onAnimationEnd(Animator animation) {
						img.setVisibility(View.INVISIBLE);
						img.clearAnimation();
					}
					@Override
					public void onAnimationCancel(Animator animation) {
						// TODO Auto-generated method stub
					}
				});
			}
		}, i);
		if(img.getId() == R.id.img_01){
			mHandler.postDelayed(new Runnable() {

				@Override
				public void run() {
					dialog.dismiss();
				}
			}, j);
		}
	}

	public static interface Sure {
		/**
		 *
		 * @param mode 1设防 2追踪 3轨迹 4围栏 5警报 6报失
		 */
		void onSure(int mode);
	}

	public Sure sure;

	/**
	 * 点击确定回调的监听
	 *
	 * @param sure
	 */
	public void setSureListener(Sure sure) {
		this.sure = sure;
	}

}
